package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.FamilyDto;
import com.zzyl.nursing.dto.QueryDeviceDataListByDayDto;
import com.zzyl.nursing.mapper.DeviceMapper;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.service.MemberUserService;
import com.zzyl.nursing.vo.MemberFamilyListByPageVo;
import com.zzyl.nursing.vo.MemberFamilyVo;
import com.zzyl.nursing.vo.QueryDeviceDataListByDayVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;

@Service
public class MemberUserServiceImpl implements MemberUserService {

    @Autowired
    private ElderMapper elderMapper;

    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private DeviceMapper deviceMapper;
    /**
     * 新增家人
     *
     * @return
     */
    @Override
    public void addMember(FamilyDto familyDto) {
        Long userId = UserThreadLocal.getUserId();

        Elder elder = elderMapper.selectOne(Wrappers.<Elder>lambdaQuery().eq(Elder::getIdCardNo, familyDto.getIdCard()));

        FamilyMemberElder familyMemberElder = new FamilyMemberElder();
        familyMemberElder.setFamilyMemberId(userId);
        familyMemberElder.setElderId(elder.getId());
        familyMemberElder.setRemark(familyDto.getRemark());

        familyMemberElderMapper.insert(familyMemberElder);
    }

    /**
     * 获取家人列表
     *
     * @return
     */
    @Override
    public List<MemberFamilyVo> my() {
        Long userId = UserThreadLocal.getUserId();
        return familyMemberElderMapper.selectElderList(userId);
    }

    /**
     * 分页获取已绑定家人列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<MemberFamilyListByPageVo> listByPage(Integer pageNum, Integer pageSize) {
        Long userId = UserThreadLocal.getUserId();
        if (ObjectUtil.isNotEmpty(userId)) {
            return familyMemberElderMapper.listByPage(userId);
        }
        return List.of();
    }

    /**
     * 按天统计查询指标数据
     *
     * @param request
     * @return
     */
    @Override
    public AjaxResult queryDeviceDataListByDay(QueryDeviceDataListByDayDto request) {
        LocalDateTime startTime = LocalDateTimeUtil.of(request.getStartTime());
        LocalDateTime endTime = LocalDateTimeUtil.of(request.getEndTime());
        List<QueryDeviceDataListByDayVo> list =  familyMemberElderMapper.queryDeviceDataListByDay(
                request.getIotId(),request.getFunctionId(),startTime,endTime);

        List<QueryDeviceDataListByDayVo> list2=new ArrayList<>();
        for(int i=0;i<24;i++){
            QueryDeviceDataListByDayVo vo = new QueryDeviceDataListByDayVo();
            vo.setDataValue(0);
            String s = "0"+i+":00";
            vo.setDateTime(s.substring(s.length()-5));
            list2.add(vo);
        }
        list2.forEach(item->{
            list.forEach(item2->{
                if(item.getDateTime().equals(item2.getDateTime())){
                    item.setDataValue(item2.getDataValue());
                }
            });
        });
        return AjaxResult.success(list2);
    }

    /**
     * 按周统计查询指标数据
     *
     * @param request
     * @return
     */
    @Override
    public AjaxResult queryDeviceDataListByWeek(QueryDeviceDataListByDayDto request) {
        LocalDateTime startTime = LocalDateTimeUtil.of(request.getStartTime());
        LocalDateTime endTime = LocalDateTimeUtil.of(request.getEndTime());
        List<QueryDeviceDataListByDayVo> list =  familyMemberElderMapper.queryDeviceDataListByWeek(
                request.getIotId(),request.getFunctionId(),startTime,endTime);

        List<QueryDeviceDataListByDayVo> list2=new ArrayList<>();


        for(int i=6;i>=0;i--){
            int mount = LocalDateTime.now().minusDays(i).getMonth().getValue();
            int dayOfMonth = LocalDateTime.now().minusDays(i).getDayOfMonth();
            QueryDeviceDataListByDayVo vo = new QueryDeviceDataListByDayVo();
            vo.setDataValue(0);
            String s1 = "0"+dayOfMonth;
            s1 = s1.substring(s1.length()-2);
            String s2 = "0"+mount+"."+s1;
            vo.setDateTime(s2.substring(s2.length()-5));
            list2.add(vo);
        }
        list2.forEach(item->{
            list.forEach(item2->{
                if(item.getDateTime().equals(item2.getDateTime())){
                    item.setDataValue(item2.getDataValue());
                }
            });
        });
        return AjaxResult.success(list2);
    }
}
